package gui;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;




public class Add extends JFrame  implements ActionListener{
	JButton button1;
	JTextField Chinesetext,Englishtext,Phonetic_signtext;
	 Connection con;
	 Statement sql;
        Add(Connection con){
        	this.con=con;
        	setLayout(null);
        	init();
        	this.setBounds(100, 150,400, 400);
        	   setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);   
        	   setVisible(true);
        }
        void init() {
        	//三文本编辑框  一个按钮
           Chinesetext= new JTextField("Chinese");
           Chinesetext.setBounds(57, 79, 100, 27);
           Englishtext=new JTextField("English");
           Englishtext.setBounds(57,134, 100, 27);
           Phonetic_signtext=new JTextField("Phonetic_signtext");
           Phonetic_signtext.setBounds(57, 200, 100, 27);
           
           button1=new JButton("增加");
           button1.setBounds(200, 134,100, 27);
           add(button1);
           add(Chinesetext);
           add(Englishtext);
           add(Phonetic_signtext);
           button1.addActionListener(this);
         
        }
        public void actionPerformed(ActionEvent e) {
           try {
			 sql=con.createStatement();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
        	if(button1==e.getSource()) {
        	     String str1=Chinesetext.getText();
        	     String str2=Englishtext.getText();
        	     String str3= Phonetic_signtext.getText();
        	     try {
					int i=sql.executeUpdate("insert into thesaurus(Chinese,English,Phonetic_sign) values('"+str1+"','"+str2+"','"+str3+"')");
				 if(i==1) {
					 JOptionPane.showMessageDialog(null, "增加成功");
				   }
        	     } catch (SQLException e1) {
					e1.printStackTrace(); 
					JOptionPane.showMessageDialog(null, "增加失败");
					
				}
        	    
        	}
        	
        }
      
}
